package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.bo.TagInfoBo;
import cn.broadsense.biz.domain.TagInfo;
import cn.broadsense.biz.dto.TagInfoTreeDto;
import cn.broadsense.biz.query.TagInfoQuery;
import cn.broadsense.biz.service.TagInfoService;
import cn.broadsense.biz.vo.TagInfoVo;
import cn.broadsense.common.annotation.Log;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.enums.BusinessType;
import cn.broadsense.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @author lijian
 * @description
 * @date 2024年09月11日 下午4:07
 */
@RestController
@Api(tags = "标签信息管理")
@RequestMapping("/tag/info")
@RequiredArgsConstructor
public class TagInfoController extends BaseController {

    private final TagInfoService tagInfoService;

    @Log(title = "标签信息管理-新增", businessType = BusinessType.INSERT)
    @ApiOperation("新增标签信息")
    @PostMapping("/add")
    public R add(@Validated @RequestBody TagInfoVo vo) {
        TagInfo one = tagInfoService.lambdaQuery().eq(TagInfo::getName, vo.getName()).last(" limit 1").one();
        if (one != null) {
            return R.fail("标签" + vo.getName() + "已存在");
        }
        tagInfoService.saveTagInfo(vo);
        return R.ok();
    }

    @Log(title = "标签信息管理-编辑", businessType = BusinessType.UPDATE)
    @ApiOperation("编辑标签信息")
    @PutMapping("/update/{id}")
    public R update(@Validated @RequestBody TagInfoVo vo, @PathVariable Long id) {
        TagInfo byId = tagInfoService.getById(id);
        if (byId == null) {
            return R.fail("标签不存在");
        }
        TagInfo one = tagInfoService.lambdaQuery().eq(TagInfo::getName, vo.getName()).ne(TagInfo::getId,id).last(" limit 1").one();
        if (one != null) {
            return R.fail("标签" + vo.getName() + "已存在");
        }
        tagInfoService.lambdaUpdate()
                .set(TagInfo::getName, vo.getName())
                .set(TagInfo::getTagRuleId,vo.getTagRuleId())
                .set(TagInfo::getTagTypeId,vo.getTagTypeId())
                .eq(TagInfo::getId, id)
                .update();
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除标签信息")
    public R delete(@PathVariable Integer id) {
        TagInfo byId = tagInfoService.getById(id);
        if (byId == null) {
            return R.fail("标签不存在");
        }
        Integer childId = tagInfoService.getChildrenList(id);
        if(Objects.nonNull(childId)){
            return R.fail("标签下有子标签，无法删除");
        }
        tagInfoService.removeById(id);
        return R.ok();
    }

    @GetMapping("/list")
    @ApiOperation("列表页")
    public R<List<TagInfoTreeDto>> list(TagInfoQuery query) {
        List<TagInfoTreeDto> listTree = tagInfoService.getListTree(query);
        return R.ok(listTree);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("标签详情")
    public R<TagInfo> detail(@PathVariable Integer id) {
        TagInfo byId = tagInfoService.getById(id);
        if (byId == null) {
            return R.fail("标签不存在");
        }
        return R.ok(tagInfoService.getTagInfo(id));
    }

    @ApiOperation("导入模板下载")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TagInfoBo> util = new ExcelUtil<TagInfoBo>(TagInfoBo.class);
        util.importTemplateExcel(response, "导入模板数据");
    }

    @PostMapping("/import")
    @ApiOperation("导入标签")
    @Log(title = "标签信息管理-导入标签", businessType = BusinessType.EXPORT)
    public R<Void> importData(MultipartFile file) throws Exception {
        ExcelUtil<TagInfoBo> util = new ExcelUtil<TagInfoBo>(TagInfoBo.class);
        List<TagInfoBo> list = util.importExcel(file.getInputStream());
        tagInfoService.importTagInfoList(list);
        return R.ok();
    }




}
